public class myQueue2 {
    //用单链表实现队列(头插尾删)

    static class IList{
        public int val;
        public IList next;
        public IList(int val){
            this.val=val;
        }
    }
    public IList head;
    //入栈
    public void offer(int val){
        IList node=new IList(val);
        if(empty()){
            head=node;
        }else{
            node.next=head;
            head=node;
        }
    }
    //出栈
    public int poll(){
        int value=0;
        if(empty()){
            return -1;
        }else {
            if(head.next==null){
                value=head.val;
                head=null;
            }else {
                IList pcur=head;
                IList ppcur=pcur.next;
                while (ppcur.next!=null){
                    pcur=ppcur;
                    ppcur=ppcur.next;
                }
                value=ppcur.val;
                pcur.next=null;
            }
        }
        return value;
    }
    //取栈顶元素
    public int peek(){
        if(empty()){
            return -1;
        }else{
            IList pcur=head;
            while (pcur.next!=null){
                pcur=pcur.next;
            }
            return pcur.val;
        }

    }
    //判空
    public boolean empty(){
        return head==null;
    }
}
